//
//  MWGoodsCell.m
//  XiRan-iOS
//
//  Created by Richfit on 2019/10/11.
//  Copyright © 2019 Wood. All rights reserved.
//

#import "MWGoodsCell.h"
#import "MWGoodsAttrCell.h"

@interface MWGoodsCell () <UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong) UIImageView *pictureIV;
@property(nonatomic, strong) UILabel *titleLB;

@property(nonatomic, strong) UILabel *priceLB;
@property(nonatomic, strong) UITableView *tableView;

@property(nonatomic, strong) UILabel *numberLB;
@property(nonatomic, strong) UILabel *showroomLB;
@property(nonatomic, strong) UILabel *sizeLB;

@property(nonatomic, strong) NSMutableArray *attrArray;

@end

@implementation MWGoodsCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.contentView.backgroundColor = [UIColor whiteColor];
        [self setupPageSubviews];
    }
    return self;
}

- (void)setupData:(MWGoods *)goods {
    NSString *urlStr = [NSString stringWithFormat:@"%@?x-oss-process=image/resize,h_360", goods.image];
//    NSString *urlStr = [NSString stringWithFormat:@"%@", goods.image];
    [_pictureIV sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"shangpin"]];
    _titleLB.text = goods.store_name;
    if (goods.price && goods.price.length > 0) {
        _priceLB.text = [NSString stringWithFormat:@"¥ %@", goods.price];
    } else {
        _priceLB.text = @"";
    }
    
    if (goods.shuxing) {
        [self.attrArray removeAllObjects];
        for (NSDictionary *dict in goods.shuxing) {
            NSString *name = dict[@"name"];
            NSString *value = dict[@"value"];
            if ((name && name.length) || (value && value.length)) {
                [self.attrArray addObject:dict];
            }
        }
        [_tableView reloadData];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.attrArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.1f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 22.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"MWGoodsAttrCell";
    MWGoodsAttrCell *attrCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!attrCell) {
        attrCell = [[MWGoodsAttrCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        attrCell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    NSDictionary *dic = self.attrArray[indexPath.row];
    attrCell.attrName = [dic objectForKey:@"name"];
    
    id value = [dic objectForKey:@"value"];
    
    if ([value isKindOfClass:[NSNumber class]]) {
        attrCell.attrValue = [value stringValue];
        
    } else {
        attrCell.attrValue = [dic objectForKey:@"value"];
    }
    return attrCell;
}

- (NSMutableArray *)attrArray {
    if (!_attrArray) {
        _attrArray = [NSMutableArray arrayWithCapacity:0];
    }
    
    return _attrArray;
}

- (void)setupPageSubviews {
    _pictureIV = [[UIImageView alloc] init];
    _pictureIV.contentMode = UIViewContentModeScaleAspectFit;
    _pictureIV.layer.cornerRadius = 5;
    _pictureIV.layer.masksToBounds = YES;
    [self.contentView addSubview:_pictureIV];
    
    _titleLB = [[UILabel alloc] init];
    _titleLB.font = MWFont(15);
    _titleLB.textColor = MWHexColor(@"#D1AF70");
    [self.contentView addSubview:_titleLB];
    
    _priceLB = [[UILabel alloc] init];
    _priceLB.font = MWFont(12);
    _priceLB.textColor = MWHexColor(@"#E42A2A");
    [self.contentView addSubview:_priceLB];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.bounces = NO;
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.contentView addSubview:_tableView];
    
    [_pictureIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView);
        make.left.equalTo(self.contentView);
        make.right.equalTo(self.contentView);
        make.height.equalTo(@120);
    }];
    
    [_titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.pictureIV.mas_bottom).offset(8);
        make.left.equalTo(self.pictureIV);
        make.right.equalTo(self.pictureIV);
        make.height.equalTo(@22);
    }];
    
    [_priceLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLB.mas_bottom);
        make.left.equalTo(self.pictureIV);
        make.right.equalTo(self.pictureIV);
    }];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.priceLB.mas_bottom);
        make.left.equalTo(self.pictureIV);
        make.right.equalTo(self.pictureIV);
        make.bottom.equalTo(self.contentView);
    }];
}

@end
